<!--  -->
<template>
  <div class="">
    <h1>登录</h1>
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="user.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="danger" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <van-divider dashed>
      <router-link to="/register">用户注册</router-link>
    </van-divider>
  </div>
</template>
    <script>
import { Toast } from "vant";
import { login } from "@/http/api";
export default {
  name: "",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
    };
  },
  components: {},
  methods: {
    onSubmit() {
      login(this.user).then((res) => {
        console.log(res);
        if (res.code != 200) {
          Toast.fail(res.msg);
          return;
        }
        // 登录成功，将返回的uid,token存在localStorage中备用
        this.$router.push("/index");
        Toast.success("登录成功");
          window.localStorage.setItem("mid", res.data.mid);
          window.localStorage.setItem("token", res.data.token);
      });
    },
  },
  created() {},
  mounted() {},
};
</script>
    <style scoped>
h1 {
  font-size: 50px;
  margin-left: 50%;
}
</style>